package com.daytask.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbManager {
	private static DbManager instance = new DbManager();

	public static DbManager getInstance() {
		return instance;
	}

	private static Connection conn = null;

	public Connection getConnection() {
		if (conn == null) {
			//System.out.println("connet db 。。。");
			String className = "com.mysql.jdbc.Driver";
			String connectUrl = "jdbc:mysql://localhost:3306/test";
			
			String username = "root";
			String password = "root";
			conn = connectDatabase(className, connectUrl, username, password);
		}
		return conn;
	}

	public Connection connectDatabase(String className, String connectUrl,
			String username, String password) {
		try {
		 
			Class.forName(className);
			  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=utf-8");
			System.out.println("Success load Driver");
			/**
			if (conn == null) {
				conn = DriverManager.getConnection(connectUrl, username,
						password);
				System.out.println("connet success");
			}
			**/

		} catch (Exception e) {

			System.out.println("Error loading jdbc driver");
			e.printStackTrace();

		}
		return conn;

	}
public static void main(String[] args) {
	DbManager.getInstance().getConnection();
}
}
